package com.cqrt.util;

/**
 * @Author：lotus
 * @DATE: 2025/7/6 14:01
 */
import com.hustcad.plm.rpm.model.dto.active.TyppmActiveTreeDTO;

import java.util.*;
import java.util.stream.Collectors;


//获取深度, 父节点, 名称相同的map
public class TreeComparatorUtil {

    // 按(深度, 父节点, 名称)分组
    public static List<MatchedPair> findMatchedNodes(
            Map<String, TyppmActiveTreeDTO> oldMap,
            Map<String, TyppmActiveTreeDTO> newMap) {


        List<MatchedPair> result = new ArrayList<>();


        Map<GroupKey, TyppmActiveTreeDTO> oldGroup = groupNodes(oldMap);
        Map<GroupKey, TyppmActiveTreeDTO> newGroup = groupNodes(newMap);


        for (GroupKey key : oldGroup.keySet()) {

            if (newGroup.containsKey(key)) {
                result.add(new MatchedPair(oldGroup.get(key), newGroup.get(key)));
            }
        }

        return result;
    }

    private static Map<GroupKey, TyppmActiveTreeDTO> groupNodes(Map<String, TyppmActiveTreeDTO> map) {
        return map.values().stream().collect(Collectors.toMap(
                        node -> {
                            String id = node.getOutLineNumber();
                            // 计算深度（点的数量）
                            int depth = id.split("\\.").length;
                            // 获取父节点ID（如果存在）
                            String parentId = null;
                            int lastDotIndex = id.lastIndexOf('.');

                            if (lastDotIndex > 0) {
                                parentId = id.substring(0, lastDotIndex);
                            }

                            return new GroupKey(depth, parentId, node.getName());
                        },
                        node -> node,
                        (existing, replacement) -> existing
                )).entrySet().stream()
                .filter(entry -> entry.getKey() != null) // 过滤掉根节点
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }

    // 分组键（深度+父节点+名称）
    static class GroupKey {
        int depth;
        String parentId;
        String name;

        public GroupKey(int depth, String parentId, String name) {
            this.depth = depth;
            this.parentId = parentId;
            this.name = name;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            GroupKey groupKey = (GroupKey) o;
            return depth == groupKey.depth &&
                    Objects.equals(parentId, groupKey.parentId) &&
                    Objects.equals(name, groupKey.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(depth, parentId, name);
        }
    }

    // 匹配结果包装类
    public static class MatchedPair {
        private final TyppmActiveTreeDTO oldNode;
        private final TyppmActiveTreeDTO newNode;

        public MatchedPair(TyppmActiveTreeDTO oldNode, TyppmActiveTreeDTO newNode) {
            this.oldNode = oldNode;
            this.newNode = newNode;
        }

        public TyppmActiveTreeDTO getOldNode() {

            return oldNode;
        }

        public TyppmActiveTreeDTO getNewNode() {
            return newNode;
        }
    }
}